package cn.longge.controller;

import cn.longge.entity.Result;
import cn.longge.entity.SysResource;
import cn.longge.entity.SysUser;
import cn.longge.service.ISysResourceService;
import cn.longge.service.ISysUserService;
import cn.longge.utils.EncryptUtils;
import javafx.beans.binding.ObjectExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class MainController {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysResourceService resourceService;

    @RequestMapping("sidebar")
    public ModelAndView getSidebar(){
        return new ModelAndView("common/sidebar");
    }

    @RequestMapping("navbar")
    public ModelAndView getNavbar(){
        return new ModelAndView("common/navbar");
    }

    @RequestMapping("login")
    @ResponseBody
    public Result login(@RequestBody Map<String, Object> params, HttpServletRequest request){
        String username = (String) params.get("username");
        String password = (String) params.get("password");
        String code = (String) params.get("code");
        HttpSession session = request.getSession();

        if(!StringUtils.isEmpty(code)){
            String checkCode = (String) session.getAttribute("checkCode");
            if(code.equals(checkCode)){
                if(!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)){
                    SysUser user = new SysUser();
                    user.setUsername(username);
                    password = EncryptUtils.MD5_HEX(EncryptUtils.MD5_HEX(password) + username);
                    user.setPassword(password);
                    SysUser select = userService.selectOne(user);
                    if(null != select){
                        session.setAttribute("loginUser",select);
                        Map<String,Object> map = new HashMap<>();
                        List<SysResource> resources = resourceService.selectByUid(select.getId());
                        session.setAttribute("resources",resources);
                        map.put("loginUser",select);
                        map.put("resources",resources);
                        return new Result(true,"登录成功",map);
                    }
                    return new Result(false,"账号密码错误",null);
                }
                return new Result(false,"账号密码不能为空",null);
            }
            return new Result(false,"验证码错误",null);
        }
        return new Result(false,"验证码不能为空",null);
    }

    @RequestMapping("toIndex")
    public ModelAndView toIndex(){
        return new ModelAndView("index");
    }

    @RequestMapping("logout")
    public String logout(HttpServletRequest request){
        request.getSession().invalidate();
        return "redirect:login2.html";
    }
}
